// pages/admin/users/users.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        users: [],
        page: 1,
        pageSize: 5,
        hasMore: true,
        cloud: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.initSharedCloud();
    },

    async initSharedCloud() {
        const cloud = new wx.cloud.Cloud({
            resourceAppid: 'wx3199c80279c7b8c3',
            resourceEnv: 'education-6gwexwp8488514df'
        });
        
        try {
            await cloud.init();
            this.setData({ cloud });
            this.loadUsers();
        } catch (err) {
            console.error('初始化云环境失败：', err);
        }
    },

    async loadUsers() {
        try {
            const { cloud, page, pageSize } = this.data;
            const db = cloud.database();
            
            // 获取总数
            const { total } = await db.collection('users').count();
            
            // 获取当前页数据
            const { data } = await db.collection('users')
                .skip((page - 1) * pageSize)
                .limit(pageSize)
                .get();
            
            this.setData({
                users: data,
                hasMore: page * pageSize < total
            });
        } catch (err) {
            console.error('加载用户失败：', err);
        }
    },

    prevPage() {
        if (this.data.page > 1) {
            this.setData({
                page: this.data.page - 1
            }, () => {
                this.loadUsers();
            });
        }
    },

    nextPage() {
        if (this.data.hasMore) {
            this.setData({
                page: this.data.page + 1
            }, () => {
                this.loadUsers();
            });
        }
    },

    handleDelete(e) {
        const userId = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要删除该用户吗？',
            success: async (res) => {
                if (res.confirm) {
                    try {
                        const { cloud } = this.data;
                        const db = cloud.database();
                        await db.collection('users').doc(userId).remove();
                        
                        // 同时删除用户相关的地址和书籍地址关联
                        await db.collection('addresses').where({
                            userId: userId
                        }).remove();
                        
                        await db.collection('bookAddresses').where({
                            userId: userId
                        }).remove();

                        wx.showToast({
                            title: '删除成功',
                            icon: 'success'
                        });
                        
                        this.loadUsers();
                    } catch (err) {
                        console.error('删除用户失败：', err);
                        wx.showToast({
                            title: '删除失败',
                            icon: 'error'
                        });
                    }
                }
            }
        });
    }
})